package com.apes.fico.engine.model;

import com.apes.framework.api.SpringManager;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.jpa.spec.JpaDsl;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

/**
 * @program: apestech
 * @description: 要素组
 * @author: Chengxm
 * @create: 2019-01-24 15:42
 **/
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = ElementGroup.CONST_TABLE_NAME)
public class ElementGroup extends Model {
    public final static String CONST_TABLE_NAME = "fin_element_group";
    @Id
    @Column(name = "id", length = 8)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "要素组")
    @Comment("编码")
    private String id;

    @Comment("名称")
    @Column(name = "name", nullable = false, length = 64)
    private String name;

    @OneToMany(mappedBy = "elementGroup", cascade = CascadeType.ALL, orphanRemoval = true)
    @OrderBy("priority asc")
    private List<ElementGroupItem> elementGroupItems = new ArrayList<>();

    public List<ElementGroupItem> getElementGroupItems() {
        for (ElementGroupItem item : elementGroupItems) {
            item.setElementGroup(this);
        }
        return elementGroupItems;
    }
}
